package com.ruoyi.light.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.domain.light.LitConfigErr;
import com.ruoyi.common.domain.light.LitConfigErrRole;
import com.ruoyi.common.mybatisplus.QueryWrTool;
import com.ruoyi.light.mapper.LitConfigErrMapper;
import com.ruoyi.light.service.ILitConfigErrRoleService;
import com.ruoyi.light.service.ILitConfigErrService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 安灯配置 - 异常类型 服务层实现
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Service
public class LitConfigErrServiceImpl extends ServiceImpl<LitConfigErrMapper, LitConfigErr> implements ILitConfigErrService {

    @Autowired
    private ILitConfigErrRoleService errRoleService;
    @Autowired
    private ISysRoleService roleService;

    @Override
    public List<LitConfigErr> getList(LitConfigErr litConfigErr) {
        List<LitConfigErr> errList = this.list(QueryWrTool.queryWrapperByBaseEntity(litConfigErr));
        List<LitConfigErrRole> errRoleList = errRoleService.list();
        Set<Long> roleIdSet = new HashSet<>();
        Map<Long, List<Long>> initErrRoleMap = new HashMap<>();
//        Map<Long, List<Long>> signErrRoleMap = new HashMap<>();
//        Map<Long, List<Long>> liftErrRoleMap = new HashMap<>();
//        Map<Long, List<Long>> verifyErrRoleMap = new HashMap<>();
        if (errRoleList.size() > 0) {
            for (LitConfigErrRole litConfigErrRole : errRoleList) {
                roleIdSet.add(litConfigErrRole.getRoleId());
                List<Long> longs = initErrRoleMap.getOrDefault(litConfigErrRole.getErrId(), new ArrayList<>());
                longs.add(litConfigErrRole.getRoleId());
                initErrRoleMap.put(litConfigErrRole.getErrId(), longs);
                /*
                switch (litConfigErrRole.getOperType()) {
                    case init -> {
                        List<Long> longs = initErrRoleMap.getOrDefault(litConfigErrRole.getErrId(), new ArrayList<>());
                        longs.add(litConfigErrRole.getRoleId());
                        initErrRoleMap.put(litConfigErrRole.getErrId(), longs);
                    }
                    case sign -> {
                        List<Long> longs = signErrRoleMap.getOrDefault(litConfigErrRole.getErrId(), new ArrayList<>());
                        longs.add(litConfigErrRole.getRoleId());
                        signErrRoleMap.put(litConfigErrRole.getErrId(), longs);
                    }
                    case lift -> {
                        List<Long> longs = liftErrRoleMap.getOrDefault(litConfigErrRole.getErrId(), new ArrayList<>());
                        longs.add(litConfigErrRole.getRoleId());
                        liftErrRoleMap.put(litConfigErrRole.getErrId(), longs);
                    }
                    case verify -> {
                        List<Long> longs = verifyErrRoleMap.getOrDefault(litConfigErrRole.getErrId(), new ArrayList<>());
                        longs.add(litConfigErrRole.getRoleId());
                        verifyErrRoleMap.put(litConfigErrRole.getErrId(), longs);
                    }
                }
                *
                 */
            }
            Map<Long, SysRole> roleMap = roleService.getRoleMap(new ArrayList<>(roleIdSet));
            for (LitConfigErr err : errList) {
                Long errId = err.getErrId();
                List<Long> initRoleIds = initErrRoleMap.getOrDefault(errId, new ArrayList<>());
                err.setRoleIdList(initRoleIds);
                if (initRoleIds.size() > 0) {
                    for (Long rid : initRoleIds) {
                        err.getRoleList().add(roleMap.get(rid));
                    }
                }
                /*
                List<Long> signRoleIds = signErrRoleMap.getOrDefault(errId, new ArrayList<>());
                err.setSignRoleIdList(signRoleIds);
                if (signRoleIds.size() > 0) {
                    for (Long rid : signRoleIds) {
                        err.getSignRoleList().add(roleMap.get(rid));
                    }
                }

                List<Long> liftRoleIds = liftErrRoleMap.getOrDefault(errId, new ArrayList<>());
                err.setLiftRoleIdList(liftRoleIds);
                if (liftRoleIds.size() > 0) {
                    for (Long rid : liftRoleIds) {
                        err.getLiftRoleList().add(roleMap.get(rid));
                    }
                }

                List<Long> verifyRoleIds = verifyErrRoleMap.getOrDefault(errId, new ArrayList<>());
                err.setVerifyRoleIdList(liftRoleIds);
                if (verifyRoleIds.size() > 0) {
                    for (Long rid : verifyRoleIds) {
                        err.getVerifyRoleList().add(roleMap.get(rid));
                    }
                }

                 */
            }
        }

        return errList;

    }

    @Override
    public LitConfigErr getOne(Long errId) {
        LitConfigErr err = this.getById(errId);
        List<LitConfigErrRole> errRoleList = errRoleService.list(Wrappers.lambdaQuery(LitConfigErrRole.class).eq(LitConfigErrRole::getErrId, errId));
        if (errRoleList.size() > 0) {
            List<Long> roleIds = errRoleList.stream().map(LitConfigErrRole::getRoleId).collect(Collectors.toList());
            Map<Long, SysRole> roleMap = roleService.getRoleMap(roleIds);
            for (LitConfigErrRole litConfigErrRole : errRoleList) {
                err.getRoleIdList().add(litConfigErrRole.getRoleId());
                err.getRoleList().add(roleMap.get(litConfigErrRole.getRoleId()));
//                switch (litConfigErrRole.getOperType()) {
//                    case init -> {
//                        err.getInitRoleIdList().add(litConfigErrRole.getRoleId());
//                        err.getInitRoleList().add(roleMap.get(litConfigErrRole.getRoleId()));
//                    }
//                    case sign -> {
//                        err.getSignRoleIdList().add(litConfigErrRole.getRoleId());
//                        err.getSignRoleList().add(roleMap.get(litConfigErrRole.getRoleId()));
//                    }
//                    case lift -> {
//                        err.getLiftRoleIdList().add(litConfigErrRole.getRoleId());
//                        err.getLiftRoleList().add(roleMap.get(litConfigErrRole.getRoleId()));
//                    }
//                    case verify -> {
//                        err.getVerifyRoleIdList().add(litConfigErrRole.getRoleId());
//                        err.getVerifyRoleList().add(roleMap.get(litConfigErrRole.getRoleId()));
//                    }
//                }
            }
        }
        return err;
    }


    @Override
    @Transactional
    public boolean addSave(LitConfigErr litConfigErr) {
        Long parentId = litConfigErr.getParentId();
        if (parentId == null || parentId == 0) {
            litConfigErr.setAncestors("0");
            litConfigErr.setParentId(0L);
            litConfigErr.setErrLv(1);
        } else {
            //存在父级
            LitConfigErr parentErr = this.getById(parentId);
            litConfigErr.setAncestors(parentErr.getAncestors() + "," + parentId);
            litConfigErr.setErrLv(parentErr.getErrLv() + 1);
        }
        this.save(litConfigErr);
        //发起人角色id
        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getRoleIdList());
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getInitRoleIdList(), LitConfigErrRole.SaveType.init);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getSignRoleIdList(), LitConfigErrRole.SaveType.sign);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getLiftRoleIdList(), LitConfigErrRole.SaveType.lift);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getVerifyRoleIdList(), LitConfigErrRole.SaveType.verify);
        return true;
    }

    @Override
    @Transactional
    public boolean editSaveById(LitConfigErr litConfigErr) {
        Long parentId = litConfigErr.getParentId();
        if (parentId == null || parentId == 0) {
            litConfigErr.setAncestors("0");
            litConfigErr.setParentId(0L);
            litConfigErr.setErrLv(1);
        } else {
            //存在父级
            LitConfigErr parentErr = this.getById(parentId);
            litConfigErr.setAncestors(parentErr.getAncestors() + "," + parentId);
            litConfigErr.setErrLv(parentErr.getErrLv() + 1);
        }
        errRoleService.remove(Wrappers.lambdaQuery(LitConfigErrRole.class).eq(LitConfigErrRole::getErrId, litConfigErr.getErrId()));
        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getRoleIdList());
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getInitRoleIdList(), LitConfigErrRole.SaveType.init);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getSignRoleIdList(), LitConfigErrRole.SaveType.sign);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getLiftRoleIdList(), LitConfigErrRole.SaveType.lift);
//        saveErrRoleBatch(litConfigErr.getErrId(), litConfigErr.getVerifyRoleIdList(), LitConfigErrRole.SaveType.verify);
        this.updateById(litConfigErr);
        return true;
    }

    private void saveErrRoleBatch(Long errId, List<Long> roleIdList) {
        List<LitConfigErrRole> errRoleList = new ArrayList<>();
        for (Long rid : roleIdList) {
            LitConfigErrRole errRole = new LitConfigErrRole(errId, rid);
            errRoleList.add(errRole);
        }
        errRoleService.saveBatch(errRoleList);
    }

    @Override
    @Transactional
    public boolean del(List<Long> idList) {
        errRoleService.remove(Wrappers.lambdaQuery(LitConfigErrRole.class).in(LitConfigErrRole::getErrId, idList));
        return this.removeByIds(idList);
    }

}